package cn.abcsys.cloud.devops.runtime.controller;

import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.abcsys.cloud.devops.runtime.bean.GridBean;
import cn.abcsys.cloud.devops.runtime.bean.Result;
import cn.abcsys.cloud.devops.runtime.entity.Host;
import cn.abcsys.cloud.devops.runtime.model.HostModel;
import cn.abcsys.cloud.devops.runtime.service.HostService;
import net.sf.json.JSONObject;


/**
 * 主机管理控制器
 * @author mayunhao
 *
 */
@RestController
@RequestMapping("/host")
public class HostController {
	private static final Logger LOGGER = Logger.getLogger(HostController.class);
	@Autowired
	private HostService hostService;
	@RequestMapping(value = "/list", method = { RequestMethod.GET })
	@ResponseBody
	public GridBean hostList(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return null;
		}else{
			HostModel hostModel = new HostModel();
			try{
				JSONObject orig = JSONObject.fromObject(params);
				hostModel = (HostModel) JSONObject.toBean(orig,HostModel.class);
			}catch(Exception e){
				LOGGER.error("请求参数转换错误："+e.getMessage());
				return null;
			}
			return hostService.getHostList(hostModel);
		}
	}
	@RequestMapping(value = "/detail", method = { RequestMethod.GET })
	@ResponseBody
	public Result getDetail(@RequestParam(value = "hostId", required = true)Integer hostId){
		return hostService.getHostDetail(hostId);
	}
	@RequestMapping(value = "/create", method = { RequestMethod.GET })
	@ResponseBody
	public Result create(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			Host host = new Host();
			try{
				JSONObject orig = JSONObject.fromObject(params);
				host = (Host) JSONObject.toBean(orig,Host.class);
			}catch(Exception e){
				LOGGER.error("请求参数转换错误："+e.getMessage());
				return new Result(false, "请求参数转换错误："+e.getMessage());
			}
			return hostService.createHost(host);
		}
	}
	@RequestMapping(value = "/modify", method = { RequestMethod.GET })
	@ResponseBody
	public Result modify(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			Host host = new Host();
			try{
				JSONObject orig = JSONObject.fromObject(params);
				host = (Host) JSONObject.toBean(orig,Host.class);
			}catch(Exception e){
				LOGGER.error("请求参数转换错误："+e.getMessage());
				return new Result(false, "请求参数转换错误："+e.getMessage());
			}
			return hostService.updateHost(host);
		}
	}
	@RequestMapping(value = "/remove", method = { RequestMethod.GET })
	@ResponseBody
	public Result remove(@RequestParam(value = "hostId", required = true)Integer hostId){
		return hostService.deleteHost(hostId);
	}
	@RequestMapping(value = "/addToCluster", method = { RequestMethod.GET })
	@ResponseBody
	public Result addToCluster(@RequestParam(value = "hostId", required = false)String hostId,
			@RequestParam(value = "clusterId", required = false)Integer clusterId,
			@RequestParam(value = "userId", required = false)Integer userId){
		return hostService.addToCluster(hostId,clusterId,userId);
	}
	@RequestMapping(value = "/removeFromCluster", method = { RequestMethod.GET })
	@ResponseBody
	public Result removeFromCluster(@RequestParam(value = "hostId", required = false)String hostId,
			@RequestParam(value = "clusterId", required = false)Integer clusterId,
			@RequestParam(value = "userId", required = false)Integer userId){
		return hostService.removeFromCluster(hostId,clusterId,userId);
	}
	/**
	 * 
	* @Title: unUsedHosts 
	* @Description: 空闲主机，可加入到集群中的kubernetes节点主机
	* @param @param params
	* @param @return    设定文件 
	* @return Result    返回类型 
	* @throws
	 */
	@RequestMapping(value = "/unUsedHosts", method = { RequestMethod.GET })
	@ResponseBody
	public Result unUsedHosts(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			Host host = new Host();
			try{
				JSONObject orig = JSONObject.fromObject(params);
				host = (Host) JSONObject.toBean(orig,Host.class);
			}catch(Exception e){
				LOGGER.error("请求参数转换错误："+e.getMessage());
				return new Result(false, "请求参数转换错误："+e.getMessage());
			}
			return hostService.unUsedHost(host);
		}
	}
	/**
	 * 
	* @Title: clusterHosts 
	* @Description: 集群中正常运行的主机
	* @param @param params
	* @param @return    设定文件 
	* @return Result    返回类型 
	* @throws
	 */
	@RequestMapping(value = "/clusterHosts", method = { RequestMethod.GET })
	@ResponseBody
	public Result clusterHosts(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			Host host = new Host();
			try{
				JSONObject orig = JSONObject.fromObject(params);
				host = (Host) JSONObject.toBean(orig,Host.class);
			}catch(Exception e){
				LOGGER.error("请求参数转换错误："+e.getMessage());
				return new Result(false, "请求参数转换错误："+e.getMessage());
			}
			return hostService.clusterHosts(host);
		}
	}
}
